<% content_for :content do %>

  <% set_active_nav params[:stat][:group_by] %>

  <!-- widget grid -->
  <section id="widget-grid" class="">
  

    <div class="row">
      <div class="col-sm-12">
        <div class="well">

          <%= simple_form_for :stat, url: stat_admin_traffics_path, :defaults => { :input_html => { :class => "form-control" } }, method: 'get', html: { class: "form-inline" } do |f| %>
            <label>设备组</label>
            <%= f.input_field :terminal_group_id, selected: params[:stat][:terminal_group_id], collection: TerminalGroup.all, label: false, prompt: '请选择设备组' %>
            &nbsp;&nbsp;    
            <label>终端</label>
            <%= f.input_field :terminal, selected: params[:stat][:terminal], collection: Terminal.includes(:terminal_user,:terminal_order,:terminal_group).all, label: false, prompt: '请选择终端' %>
            &nbsp;&nbsp;
            <label>开始-结束时间</label>
            <div class="form-group string">
              <%= f.text_field :start_date, class: 'form-control datepicker', "data-dateformat" => "yy-mm-dd",  value: params[:stat][:start_date], placeholder: "From" %>
            </div>
            <div class="form-group string">
              <%= f.text_field :end_date, class: 'form-control datepicker', "data-dateformat" => "yy-mm-dd", value: params[:stat][:end_date], placeholder: "To" %>
            </div>
            <%= f.hidden_field :group_by,value: params[:stat][:group_by]%>
            <%= f.submit "搜索", :class => "btn btn-default"%>
          <% end %>

        </div> <!-- end well div -->

      </div>
    </div>
    
    <!-- row -->
    <div class="row">
      <% if @stats %>
      <!-- NEW WIDGET START -->
      <article class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        

        <!-- Widget ID (each widget will need unique ID)-->
        <div class="jarviswidget jarviswidget-color-darken" id="wid-id-1" data-widget-editbutton="false" data-widget-colorbutton="false" data-widget-deletebutton="false">
          <!-- widget options:
          usage: <div class="jarviswidget" id="wid-id-0" data-widget-editbutton="false">

          data-widget-colorbutton="false"
          data-widget-editbutton="false"
          data-widget-togglebutton="false"
          data-widget-deletebutton="false"
          data-widget-fullscreenbutton="false"
          data-widget-custombutton="false"
          data-widget-collapsed="true"
          data-widget-sortable="false"

          -->
          <header>
            <ul class="nav nav-tabs">
              <li<%= active_nav 'day' %>><%= link_to '日', url_for(add_group_by_params 'day') %></li>
              <li<%= active_nav 'week' %>><%= link_to '周', url_for(add_group_by_params 'week') %></li>
              <li<%= active_nav 'month' %>><%= link_to '月', url_for(add_group_by_params 'month') %></li>
            </ul>
          </header>

          <!-- widget div-->
          <div>

            <!-- widget edit box -->
            <div class="jarviswidget-editbox">
              <!-- This area used as dropdown edit box -->
            </div>
            <!-- end widget edit box -->
            
            <!-- widget content -->
            <div class="widget-body no-padding">

              <div class="alert alert-info no-margin">
                <span class="">
                  <i class="fa-fw fa fa-info"></i>共有<%= @stats.total_count if @stats %>条记录
                </span>
                <span class="col-md-offset">
                  <a class="btn btn-default btn-sm" href="<%= stat_admin_traffics_path({format: :csv}.merge!(params)) %>">导出为CSV</a>
                </span>
              </div>

              <table class="table table-bordered table-striped">
                <thead>
                  <tr>
                    <th>日期</th>
                    <th>设备组</th>
                    <th>终端</th>
                    <th>流量</th>
                    <th>流量明细</th>
                  </tr>
                </thead>
                <tbody>
                <% @stats.each do |stat| %>
                  <tr id='stat-<%= stat.id %>'>
                    <td>
                      <%= stat.key %>
                    </td>
                    <td><%= stat.terminal.try(:terminal_group).try(:group_name) %></td>
                    <td><%= stat.terminal.mac %></td>
                    <td>
                      <%= traffics_friendly_format(stat.flow.to_i) %>
                    </td>
                    <td><%= link_to '流量明细', detail_admin_traffics_path(start_date: stat.start_date,end_date: stat.end_date,terminal_group_id: params[:stat][:terminal_group_id],device_id: stat.terminal_id), remote: true %></td>
                  </tr>
                <% end %>
                </tbody>
              </table>

              <div class="dt-row dt-bottom-row"><div class="row"><div class="col-sm-6"><div class="dataTables_info" id="datatable_fixed_column_info"></div></div><div class="col-sm-6 text-right"><%= paginate @stats, :params => {stat: {group_by: params[:stat][:group_by]}} %></div></div></div>

            </div>
            <!-- end widget content -->

          </div>
          <!-- end widget div -->

        </div>
        <!-- end widget -->
  
      </article>
      <!-- WIDGET END -->
    <% else %>
      <div class="col-sm-12">
        <div class="alert alert-warning">
          <i class="fa-fw fa fa-warning"></i>
          <strong>Warning</strong> 请选择终端或设备组.
        </div>
      </div>
    <% end %> 
    </div>
  
    <!-- end row -->
 
  </section>
  <!-- end widget grid -->
<% end %>